package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.SmartBarExListAdapter;
import com.example.smartcity.ui.Bean.SMartBarStopBean;
import com.example.smartcity.ui.Bean.SmartBarMesBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SmartBar extends AppCompatActivity {

    private String TAG = "TAG";
    private ExpandableListView SmartBarListView;
    ArrayList<ArrayList<String>> group = new ArrayList<>();
    ArrayList<ArrayList<String>> item = new ArrayList<>();

    ArrayList<String> nameList = new ArrayList<>();
    ArrayList<String> firstList = new ArrayList<>();
    ArrayList<String> endList = new ArrayList<>();
    ArrayList<String> priceList = new ArrayList<>();
    ArrayList<String> mileageList = new ArrayList<>();
    ArrayList<String> startTimeList = new ArrayList<>();
    ArrayList<String> endTimeList = new ArrayList<>();
    ArrayList<String> linesIdList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_bar);
        SmartBarListView = findViewById(R.id.SmartBarListView);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){



        String newUrl = OkHttpUtils.getBaseUrl(SmartBar.this) + "/userinfo/lines/list?pageNum=1&pageSize=10";
        OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Gson gson = new Gson();
                ArrayList<SmartBarMesBean.RowsBean> tempList = new ArrayList<>();
                SmartBarMesBean bean = gson.fromJson(json,SmartBarMesBean.class);
                List<SmartBarMesBean.RowsBean> list = bean.getRows();
                if (list!=null){
                    tempList.addAll(list);
                }
                for (int i = 0; i < tempList.size(); i++) {
                    nameList.add(tempList.get(i).getName());
                    firstList.add(tempList.get(i).getFirst());
                    endList.add(tempList.get(i).getEnd());
                    priceList.add(String.valueOf(tempList.get(i).getPrice()));
                    mileageList.add(tempList.get(i).getMileage());
                    startTimeList.add(tempList.get(i).getStartTime());
                    endTimeList.add(tempList.get(i).getEndTime());
                    linesIdList.add(String.valueOf(tempList.get(i).getId()));
                }
                group.add(nameList);
                group.add(firstList);
                group.add(endList);
                group.add(priceList);
                group.add(mileageList);
                group.add(startTimeList);
                group.add(endTimeList);

                //获取子项数据
                for (int i = 0; i < linesIdList.size(); i++) {
                    ArrayList<SMartBarStopBean.RowsBean> StopTempList = new ArrayList<>();
                    ArrayList<String> QnameList = new ArrayList<>();
                    String newUrl = OkHttpUtils.getBaseUrl(SmartBar.this) + "/userinfo/busStop/list?pageNum=1&pageSize=10&linesId="+linesIdList.get(i);
                    Response response1 = OkHttpUtils.getCall(newUrl).execute();
                    String stopJson = response1.body().string();

                    SMartBarStopBean stopBean = gson.fromJson(stopJson,SMartBarStopBean.class);
                    List<SMartBarStopBean.RowsBean> stopList = stopBean.getRows();
                    if (stopList!=null){
                        StopTempList.addAll(stopList);
                    }
                    for (int j = 0; j < StopTempList.size(); j++) {
                        QnameList.add(StopTempList.get(j).getName());
                    }
                    item.add(QnameList);
                }
                //设置适配器
                SmartBarExListAdapter adapter = new SmartBarExListAdapter(group,item);
                SmartBarListView.post(new Runnable() {
                    @Override
                    public void run() {
                        SmartBarListView.setAdapter(adapter);
                    }
                });
            }
        });
        SmartBarListView.setGroupIndicator(null);
        SmartBarListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                TextView textView = v.findViewById(R.id.TV_name);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(SmartBar.this,SmartBarMessage.class);
                        intent.putExtra("LinesId",linesIdList.get(groupPosition));
                        intent.putStringArrayListExtra("StopMessageList",item.get(Integer.valueOf(linesIdList.get(groupPosition))-1));
                        startActivity(intent);
                    }
                });
                return false;
            }
        });
    }
}